<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Repaylink
 *
 * @author wupeng
 */

include_once 'ApiAbstract.php';
include_once 'Rechange.php';

class Repaylink extends ApiAbstract {
    //put your code here
    
    public function __construct($trade_no, $paybill, $periodStart, $account) {
        $this->_tradeNo = $trade_no;
        $this->_paybill = $paybill;
        parent::__construct($periodStart, $account);
        $this->_fields = array(
            'transaction_number' => $trade_no,
            'trader' => 'backend-account-dr',
            'customer_id' => $paybill->AccountId,
            'member' => 'admin',
            'amount' => $paybill->Money,
            'currency' => 'CNY',
            'rate' => '1',
            'rate_amount' => '0',
            'period_start' => $periodStart,
            'period_end' => $account->Money,
            'business_type' => 'CZJE',
            'order_sn' => ''
        );
    }

    protected function _addRechangeInfo() {
        $rechange = new Rechange($this->_tradeNo, $this->_paybill, $this->_periodStart, $this->_account);
        $rechange->addTransaction();
        $account = Application_Model_UserAccount::load($this->_account->id);
        $this->_periodStart = $account->Money;
        return $this;
    }
    
    protected function _getTrades() {
        return array(
            Application_Model_PayBill::PAY_PAYPAL => 'other_Paypal',
            Application_Model_PayBill::PAY_ALIPAY => 'other_alipay',
            Application_Model_PayBill::PAY_CARD => 'xinyongka',
            Application_Model_PayBill::PAY_ACCOUNT => 'backend-account-dr'
        );
        
    }
    
    public function addTransaction() {
        $trades = $this->_getTrades();
        $this->setFields('trader',$trades[$this->_paybill->payId]);
        $periodStart = $this->_periodStart;
        if ($this->_paybill->payId != Application_Model_PayBill::PAY_ACCOUNT) {
            $this->_addRechangeInfo();
            $periodStart = $this->_account->Money;
        }
        
        $repaylink = Application_Model_RepayLink::load($this->_paybill->DomainId);
        if ($repaylink) {
            if (Application_Model_PayBill::TYPE_Visiting == $repaylink->type) { //上门海外补交款
                $visiting = Application_Model_VisitingServer::load($repaylink->relationId);
                $user = Application_Model_User::load($this->_paybill->AccountId);
                $this->setFields('business_type', 'smhwbjk')
                        ->setFields('amount', $repaylink->repayMoney)
                        ->setFields('period_start',$periodStart)
                        ->setFields('period_end', $periodStart - $repaylink->repayMoney)
                        ->setFields('customer_name',$user->nikename)
                        ->setFields('order_sn',$visiting->visitingNo);
                $url = $this->_buildUrl('addTransaction');

                $this->_updateMageFinance($url, $this->getFields());
            }
        }
        return $this;
    }
    
}
